package org.mms.controller;

import org.mms.pojo.dto.MaterialTypeDto;
import org.mms.pojo.entity.MaterialType;
import org.mms.pojo.request.MaterialTypeRequest;
import org.mms.pojo.util.ResponseMessage;
import org.mms.service.MaterialTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

        import java.util.List;

@RestController
@RequestMapping("/api/material-types")
public class MaterialTypeController {
    @Autowired
    private MaterialTypeService materialTypeService;

    @GetMapping
    public ResponseMessage<List<MaterialTypeDto>> list() {
        return ResponseMessage.success(materialTypeService.list()
                .stream()
                .map(MaterialType::convert)
                .toList());
    }

    @GetMapping("/{id}")
    public ResponseMessage<MaterialTypeDto> getById(@PathVariable Long id) {
        return ResponseMessage.success(materialTypeService.getById(id)
                .convert());
    }

    @PostMapping
    public ResponseMessage<MaterialTypeDto> create(@RequestBody MaterialTypeRequest request) {
        return ResponseMessage.success(materialTypeService.create(request)
                .convert());
    }

    @PutMapping
    public ResponseMessage<MaterialTypeDto> update(@RequestBody MaterialTypeRequest request) {
        return ResponseMessage.success(materialTypeService.update(request)
                .convert());
    }

    @DeleteMapping("/{id}")
    public ResponseMessage<MaterialTypeDto> delete(@PathVariable Long id) {
        materialTypeService.delete(id);
        return ResponseMessage.success(null);
    }
}